<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="id">
        <el-input v-model="dataForm.id" placeholder="身份证号"></el-input>
      </el-form-item>
      <el-form-item label="驾照编号" prop="dlNumber">
        <el-input v-model="dataForm.dlNumber" placeholder="驾照编号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.confirmPassword" type="password" placeholder="确认密码"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validateConfirmPassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('确认密码不能为空'))
        } else if (this.dataForm.password !== value) {
          callback(new Error('确认密码与密码输入不一致'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userName: '',
          id: '',
          dlNumber: '',
          password: '',
          confirmPassword: '',
          salt: '',
          email: '',
          mobile: '',
          status: 1
        },
        dataRule: {
          userName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' },
            { pattern: /^[\u4e00-\u9fa5]{2,}$/, message: '姓名必须是2位长度以上的汉字', trigger: 'blur' }
          ],
          id: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' },
            { pattern: /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/, message: '请输入合法的身份证号', trigger: 'blur' }
          ],
          dlNumber: [
            { required: true, message: '驾照编号不能为空', trigger: 'blur' },
            { pattern: /^\d{12}$/, message: '驾照编号必须是12位长的数字', trigger: 'blur' }
          ],
          password: [
            { min: 4, message: '密码最小长度为4', trigger: 'blur' },
            { max: 16, message: '密码最大长度为16', trigger: 'blur' },
            { pattern: /^[a-zA-Z0-9]{4,16}$/, message: '密码必须由字母或数字组成', trigger: 'blur' }
          ],
          confirmPassword: [
            { validator: validateConfirmPassword, trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' },
            { pattern: /^[A-Za-z0-9+_.-]+@(.+)$/, message: '邮箱格式错误', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (userId) {
        this.dataForm.userId = userId || 0
        this.visible = true
        this.$refs.dataForm.resetFields()
        if (this.dataForm.userId) {
          this.$refs.dataForm.clearValidate()
          this.$http({
            url: this.$http.adornUrl(`/mvs/owner/info/${this.dataForm.userId}`),
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.userName = data.user.username
              this.dataForm.id = data.user.id || ''
              this.dataForm.dlNumber = data.user.dlNumber
              this.dataForm.salt = data.user.salt
              this.dataForm.email = data.user.email
              this.dataForm.mobile = data.user.mobile
              this.dataForm.status = data.user.status
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mvs/owner/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'id': this.dataForm.id,
                'dlNumber': this.dataForm.dlNumber,
                'username': this.dataForm.userName,
                'password': this.dataForm.password,
                'salt': this.dataForm.salt,
                'email': this.dataForm.email,
                'mobile': this.dataForm.mobile,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
